package fr.zadoka.zadokadna.db;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.provider.BaseColumns;
import android.util.Log;

/**
 * 
 * The Zadoka Database
 * Gives direct access to the tables
 *
 */
public class ZadokaDatabase {
	
    private static final String TAG = "fr.zadoka.zadokadna.ZadokaDatabase";
    
	private final Context mContext;
    private ZadokaDBHelper mDbHelper;
	private SQLiteDatabase mDatabase;
    
    
	public ZadokaDatabase(Context context) {
        mContext = context;
    }

	public ZadokaDatabase open()
	{
		mDbHelper = new ZadokaDBHelper(mContext, 
										ZadokaDatabaseContract.DATABASE_NAME, 
										null, 
										ZadokaDatabaseContract.DATABASE_VERSION);
        mDatabase = mDbHelper.getWritableDatabase();
        return this;
    }	
	
	public void close()
	{
		mDbHelper.close();
	} 
	
	public long insertFavorite(ContentValues values)
	{
		return mDatabase.insert(ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME, null, values);
	}
	
	public int deleteFavorite(String whereClause, String[] whereArgs)
	{
		return mDatabase.delete(ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME, whereClause, whereArgs);
	}
	
	public Cursor getFavoriteCursor(String[] projection, String selection, String[] selectionArgs, String sortOrder)
	{
		Cursor cursor = mDatabase.query(ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME, 
				projection, selection , selectionArgs, null, null, sortOrder);

		return cursor;				
	}
	   
    private static class ZadokaDBHelper extends SQLiteOpenHelper{

        @Override
		public void onOpen(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			super.onOpen(db);
            Log.i(TAG, "Opening database "+db.getPath());
		}

/*		private static final String ZADOKA_TABLE_CREATE =
                "CREATE TABLE " + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME + " (" +
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_DATE + " TEXT, " +
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_LINK + " TEXT, "+
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IS_FAVORITE + " INTEGER );";*/
		
		private static final String ZADOKA_TABLE_ALTER =
				"ALTER TABLE " + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME +
				" ADD " + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IS_FAVORITE + " INTEGER;";
		
		private static final String FAVORITE_TABLE_RENAME_TO_ZADOKA = 
				"ALTER TABLE " + ZadokaDatabaseContract.ZadokaFavorites.FAVORITE_TABLE_NAME +
				" RENAME TO " + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME + " ;";
		
		private static final String ZADOKA_TABLE_RENAME_TO_TMP =
				"ALTER TABLE " + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME +
				" RENAME TO " + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME + "_TMP ;";
				
		private static final String ZADOKA_TABLE_CREATE_V6 =
                "CREATE TABLE " + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME + " (" +
                		BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_DATE + " TEXT, " +
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_LINK + " TEXT, "+
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IS_FAVORITE + " INTEGER );";

		private static final String ZADOKA_TABLE_FILL_V6_TABLE =
				"INSERT INTO "  + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME + " ("+
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_DATE + " , " +
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_LINK + " , "+
                		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IS_FAVORITE + "  )"+
				"SELECT "+
        		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_DATE + " , " +
        		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IMAGE_LINK + " , "+
        		ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_IS_FAVORITE + " "+
				"FROM " + ZadokaDatabaseContract.ZadokaFavoriteContract.ZADOKA_DNA_TABLE_NAME + "_TMP";
		
		public ZadokaDBHelper(Context context, String name,
				CursorFactory factory, int version) {
	        super(context, name, factory, version);
		}
		
	    @Override
	    public void onCreate(SQLiteDatabase db) {
            Log.i(TAG, "Creating database "+db.getPath());
	        db.execSQL(ZADOKA_TABLE_CREATE_V6);
	    }

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will update all old data");
            if (oldVersion == 5)
            {
            	db.execSQL(ZADOKA_TABLE_RENAME_TO_TMP);
            	db.execSQL(ZADOKA_TABLE_CREATE_V6);
            	db.execSQL(ZADOKA_TABLE_FILL_V6_TABLE);            	
            }
            else if (oldVersion < 5)
            {
            	db.execSQL(FAVORITE_TABLE_RENAME_TO_ZADOKA);
            	db.execSQL(ZADOKA_TABLE_ALTER);            	
            }
		}		
    }
    

}
